package zone.haiwen.demo.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zone.haiwen.demo.domain.entity.vo.Myuser;
import zone.haiwen.demo.service.IMyuserService;
import zone.haiwen.demo.util.JsonResult;
import zone.haiwen.demo.util.ResultCode;

import java.util.List;

/**
 * Description: 前端控制器
 * @author zhangHaiWen
 * @data 2021/7/7 19:50
 * @return
 **/
@RestController
@RequestMapping("/test/myuser")
public class MyuserController {

    private final IMyuserService myuserService;

	public MyuserController(IMyuserService myuserService) {
		this.myuserService = myuserService;
	}

	@GetMapping("/hello")
	public String sayHello() {
		return "hello";
	}

	@ApiOperation(value = "获得用户列表", httpMethod = "GET")
	@RequestMapping(value = "/listUser")
	public JsonResult listUser() {
		// 框架自带的方法
		List<Myuser> list = this.myuserService.list(null);
		return new JsonResult(ResultCode.SUCCESS, list);
	}

	@ApiOperation(value = "根据用户名获得用户记录", notes = "名称不能为空", httpMethod = "GET")
	@ApiImplicitParam(dataType = "string", name = "name", value = "用户名", required = true)
	@RequestMapping(value = "/getUserByName")
	public JsonResult getUserByName(String name) {
		// 自定义的方法
		Myuser myuser = this.myuserService.getUserByName(name);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}

	@GetMapping(value = "/getUserByAge")
	public JsonResult getUserByAge(Integer age) {
		List<Myuser> myusers = this.myuserService.getUserByAge(age);
		return new JsonResult(ResultCode.SUCCESS, myusers);
	}

	@GetMapping(value = "/insertUser")
	public JsonResult insertUser(Long id, String name, Integer age, String email) {
		this.myuserService.insertUser(id, name, age, email);
		List<Myuser> list = this.myuserService.list(null);
		return new JsonResult(ResultCode.SUCCESS, list);
	}

	@GetMapping(value = "/updateUser")
	public JsonResult updateUser(Long id, String name, Integer age, String email){
		this.myuserService.updateUser(id, name, age, email);
		List<Myuser> list = this.myuserService.list(null);
		return new JsonResult(ResultCode.SUCCESS, list);
	}

	@GetMapping(value = "/deleteUserById")
	public JsonResult deleteUserById(Long id) {
		this.myuserService.deleteUserById(id);
	    List<Myuser> list = this.myuserService.list(null);
	    return new JsonResult(ResultCode.SUCCESS, list);
	}

	@GetMapping(value = "/queryUserIf")
	public JsonResult queryUserIf(Long id, String name) {
		Myuser myuser = this.myuserService.queryUserIf(id, name);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}

	@GetMapping(value = "/queryUserChoose")
	public JsonResult queryUserChoose(String name, Integer age) {
		List<Myuser> myuser = this.myuserService.queryUserChoose(name, age);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}

	@GetMapping(value = "/queryUserWhere")
	public JsonResult queryUserWhere(String name, Integer age) {
		List<Myuser> myuser = this.myuserService.queryUserWhere(name, age);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}

	@GetMapping(value = "/queryUserForeach")
	public JsonResult queryUserForeach(List<Long> list) {

		List<Myuser> myuser = this.myuserService.queryUserForeach(list);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}
}
